<?php 
class Tools {}


function debug($var){

	if(NZ_Config::$debug>0){
		$debug = debug_backtrace();
		echo '<p>&nbsp;</p><p><a href="#" onclick="$(this).parent().next(\'ol\').slideToggle(); return false;"><strong>'.$debug[0]['file'].' </strong> l.'.$debug[0]['line'].'</a></p>'; 
		echo '<ol style="display:none;">'; 
		foreach($debug as $k=>$v){ if($k>0){
			if (isset($v['file']))
                echo '<li><strong>'.$v['file'].' </strong> l.'.$v['line'].'</li>'; 
		}}
		echo '</ol>'; 
		echo '<pre>';
		print_r($var);
		echo '</pre>'; 
	}
	
}

function pSQL($string, $htmlOK = false)
{
	return escape($string, $htmlOK);
}

/**
 * Sanitize data which will be injected into SQL query
 *
 * @param string $string SQL data which will be injected into SQL query
 * @param boolean $html_ok Does data contain HTML code ? (optional)
 * @return string Sanitized data
 */
function escape($string, $html_ok = false)
{
	if (get_magic_quotes_gpc())
		$string = stripslashes($string);
	if (!is_numeric($string))
	{
    	$search = array("\\", "\0", "\n", "\r", "\x1a", "'", '"');
    	$replace = array("\\\\", "\\0", "\\n", "\\r", "\Z", "\'", '\"');		
    	$string = str_replace($search, $replace, $string);
        
		if (!$html_ok)
			$string = strip_tags(nl2br($string));
	}

	return $string;
}

function format_date($format = '', $timestamp, $type = 'medium', $timezone = NULL, $langcode = NULL) {
    /*// Use the advanced drupal_static() pattern, since this is called very often.
    static $drupal_static_fast;
    if (!isset($drupal_static_fast)) {
        $drupal_static_fast['timezones'] = &drupal_static(__FUNCTION__);
    }
    $timezones = &$drupal_static_fast['timezones'];*/
    $timezones = array();
    
    if (!isset($timezone)) {
        $timezone = date_default_timezone_get();
    }
    // Store DateTimeZone objects in an array rather than repeatedly
    // constructing identical objects over the life of a request.
    if (!isset($timezones[$timezone])) {
        $timezones[$timezone] = timezone_open($timezone);
    }
    
    // Use the default langcode if none is set.
    /*global $language;*/
    $lang = NZ_Session::getInstance()->userdata('lang');
    if (empty($langcode)) {
        /*$langcode = isset($language->language) ? $language->language : 'en';*/
        $langcode = !empty($lang) ? $lang : 'en';
    }
    
    switch ($type) {
        case 'short':
            $format = 'm/d/Y - H:i';
            break;
        
        case 'long':
            $format = 'l, F j, Y - H:i';
            break;
        
        case 'custom':
            // No change to format.
            break;
        
        case 'medium':
        default:
            // Retrieve the format of the custom $type passed.
            if ($type != 'medium') {
                $format = '';
            }
            // Fall back to 'medium'.
            if ($format === '') {
                $format = 'D, m/d/Y - H:i';
            }
            break;
    }
    
    // Create a DateTime object from the timestamp.
    $date_time = date_create('@' . $timestamp);
    // Set the time zone for the DateTime object.
    date_timezone_set($date_time, $timezones[$timezone]);
    
    // Encode markers that should be translated. 'A' becomes '\xEF\AA\xFF'.
    // xEF and xFF are invalid UTF-8 sequences, and we assume they are not in the
    // input string.
    // Paired backslashes are isolated to prevent errors in read-ahead evaluation.
    // The read-ahead expression ensures that A matches, but not \A.
    $format = preg_replace(array('/\\\\\\\\/', '/(?<!\\\\)([AaeDlMTF])/'), array("\xEF\\\\\\\\\xFF", "\xEF\\\\\$1\$1\xFF"), $format);
    
    // Call date_format().
    $format = date_format($date_time, $format);
    
    // Pass the langcode to _format_date_callback().
    _format_date_callback(NULL, $langcode);
    
    // Translate the marked sequences.
    return preg_replace_callback('/\xEF([AaeDlMTF]?)(.*?)\xFF/', '_format_date_callback', $format);
}

function _format_date_callback(array $matches = NULL, $new_langcode = NULL) {
    // We cache translations to avoid redundant and rather costly calls to t().
    static $cache, $langcode;
    
    if (!isset($matches)) {
        $langcode = $new_langcode;
        return;
    }
    
    $code = $matches[1];
    $string = $matches[2];
    
    if (!isset($cache[$langcode][$code][$string])) {
        $options = array(
            'langcode' => $langcode,
        );
        
        if ($code == 'F') {
            $options['context'] = 'Long month name';
        }
        
        if ($code == '') {
            $cache[$langcode][$code][$string] = $string;
        }
        else {
            $cache[$langcode][$code][$string] = Translate::getTranslation($string);
        }
    }
    return $cache[$langcode][$code][$string];
}



